package com.yanqu.road.server.gameplayer.module.inn;

import com.yanqu.road.entity.config.inn.DishesInfo;
import com.yanqu.road.entity.config.patrons.PatronsInfo;
import com.yanqu.road.entity.config.skill.SkillInfo;
import com.yanqu.road.entity.config.skill.SkillUpgradeInfo;
import com.yanqu.road.entity.curio.enums.eCurioConditionType;
import com.yanqu.road.entity.enums.eGamePlayerEventType;
import com.yanqu.road.entity.enums.eItemId;
import com.yanqu.road.entity.enums.eLogMoneyType;
import com.yanqu.road.entity.enums.eSkillType;
import com.yanqu.road.entity.inn.InnEndCookResult;
import com.yanqu.road.entity.inn.InnScore;
import com.yanqu.road.entity.inn.UserInnInfo;
import com.yanqu.road.entity.inn.UserDishes;
import com.yanqu.road.entity.log.LogDishesUpgrade;
import com.yanqu.road.entity.log.LogInnCook;
import com.yanqu.road.entity.log.LogPatronsCook;
import com.yanqu.road.entity.player.UserPatrons;
import com.yanqu.road.entity.task.args.union.UnionActivityTaskArgs;
import com.yanqu.road.logic.bussiness.player.UserInnBussiness;
import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.logic.pb.InnPb;
import com.yanqu.road.pb.inn.InnProto;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.curio.CurioModule;
import com.yanqu.road.server.gameplayer.module.base.GeneralModule;
import com.yanqu.road.server.gameplayer.module.patrons.PatronsModule;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.gameplayer.module.xiaomian.XiaoMianModule;
import com.yanqu.road.server.manger.ConfigMgr;
import com.yanqu.road.server.manger.activity.curio.CurioMgr;
import com.yanqu.road.server.manger.config.DishesMgr;
import com.yanqu.road.server.manger.config.PatronsMgr;
import com.yanqu.road.server.manger.config.SkillMgr;
import com.yanqu.road.server.manger.log.AutoLogMgr;
import com.yanqu.road.server.manger.log.LogMgr;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.date.DateHelper;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;


public class InnModule extends GeneralModule {
    private static Logger logger = LogManager.getLogger(InnModule.class.getName());
    private Map<Integer, UserDishes> userDishesMap; //玩家拥有的菜肴

    private UserInnInfo userInnInfo;  //客栈信息

    public InnModule(GamePlayer player) {
        super(player);
    }

    @Override
    public boolean loadData() {
        userDishesMap = UserInnBussiness.getUserDishesMap(player.getUserId());
        //没初始化时为null
        userInnInfo = UserInnBussiness.getUserCook(player.getUserId());

        if (null == userInnInfo) {
            userInnInfo = initUserCookInfo();
        }
        return true;
    }

    @Override
    public boolean afterLoadData() {
        return true;
    }

    @Override
    public boolean saveData() {
        if (null != userInnInfo) {
            if (userInnInfo.isInsertOption()) {
                UserInnBussiness.addUserCook(userInnInfo);
            } else if (userInnInfo.isUpdateOption()) {
                UserInnBussiness.updateUserCook(userInnInfo);
            }
        }

        if (null != userDishesMap && userDishesMap.size() > 0) {
            for (UserDishes userDishes : userDishesMap.values()) {
                if (userDishes.isInsertOption()) {
                    UserInnBussiness.addUserDishes(userDishes);
                } else if (userDishes.isUpdateOption()) {
                    UserInnBussiness.updateUserDishes(userDishes);
                }
            }
        }
        return true;
    }

    @Override
    public void afterLogin() {
        //刷新全部菜谱
        syncAllDishesData();
        syncUserCookInfo();
    }


    //系统解锁 初始化数据
    public void initInnSystem() {
        //解锁刷新门客技能数据
        player.getModule(PatronsModule.class).flushPatronsSkill();
    }

    /**
     * 获取客栈信息
     *
     * @return
     */
    public InnProto.InnDetailInfoMsg getInnInfo() {
        if (null == userInnInfo) {
            return null;
        }

        return InnPb.parseInnInfo(userInnInfo, getEndCookExp());
    }

    /**
     * 创建烹饪
     *
     * @param dishesId
     * @param patronsId
     * @param num
     * @return
     */
    public short startCook(int dishesId, int patronsId, int num) {
        if (isCook()) {
            return GameErrorCode.E_INN_COOK_STATE_START;  // 烹饪已开始
        }
        if (num <= 0) {
            return GameErrorCode.E_REQUEST_ARGUMENT_ERROR;
        }
        //菜品是否存在
        DishesInfo dishes = DishesMgr.getDishes(dishesId);
        if (null == dishes) {
            return GameErrorCode.E_INN_DISHES_NO_FOUND;
        }

        //门客职业 菜品的类型
        UserPatrons userPatrons = player.getModule(PatronsModule.class).getUserPatrons(patronsId);
        PatronsInfo patronsInfo = PatronsMgr.getPatronsInfo(patronsId);
        if (null == patronsInfo || null == userPatrons) {
            return GameErrorCode.E_PATRONS_NO_FOUND;
        }
        //菜品和门客不符
        if (!DishesMgr.getOccupationByDishesType(dishes.getDishesType()).contains(patronsInfo.getOccupation())) {
            return GameErrorCode.E_INN_DISHES_PATRONS_NOT_FIT;
        }
        //消耗材料
        Property consume = PropertyHelper.parseStringToProperty(dishes.getConsume());
        //数量
        consume.rideProperty(num);
        if (!player.getModule(CurrencyModule.class).removeCurrency(consume, eLogMoneyType.Inn, eLogMoneyType.InnCookConsume)) {
            return GameErrorCode.E_BAG_ITEM_NO_ENOUGH;
        }
        //初始化新的烹饪信息
        startUserCookInfo(dishes, patronsInfo, num);


        //厨艺加成系数 千分
        Map<Integer, Integer> todayHootDishes = getHotDishes();
        if (null != todayHootDishes && todayHootDishes.containsKey(dishes.getDishesType())) {
            userInnInfo.setHotRate(todayHootDishes.get(dishes.getDishesType()));
        }
        //同步客栈信息
        //syncUserCookInfo();
        long time = dishes.getCookSecond() * num;
        AutoLogMgr.add(new LogInnCook(getUserId(), patronsId, dishesId, dishes.getDishesType(), userInnInfo.isHot() ? 1 : 0, num, PropertyHelper.parsePropertyToString(consume), time, userInnInfo.getCookStartTime() * 1000));
        //创建完毕

        //记录总烹饪次数
        userInnInfo.setTotalCookTimes(userInnInfo.getTotalCookTimes() + userInnInfo.getDishesNum());

        //成就、任务
        player.notifyListener(eGamePlayerEventType.InnCookTimes.getValue(), num);
        player.notifyListener(eGamePlayerEventType.InnTotalCookTimes.getValue(), userInnInfo.getTotalCookTimes());
        //小面任务
        player.getModule(XiaoMianModule.class).addInnCookTimes(num);
        player.notifyListener(eGamePlayerEventType.DAY_FIRST_INN.getValue());

        player.getModule(CurioModule.class).addUserCurioCountData(eCurioConditionType.InnCookTime.getType(), 1);
        return 0;
    }


    /**
     * 结束烹饪获取奖励  设置经营状态关闭
     */
    public InnEndCookResult getCookReword() {

        InnEndCookResult result = new InnEndCookResult();
        result.setRet(0);

        if (!isCook()) {
            result.setRet(GameErrorCode.E_INN_COOK_STATE_END);
            return result; // 烹饪已经结束
        }

        long currentTime = System.currentTimeMillis() / 1000;
        long cookStartTime = userInnInfo.getCookStartTime();
        DishesInfo dishesInfo = DishesMgr.getDishes(userInnInfo.getDishesId());

        if (dishesInfo == null) {
            result.setRet(GameErrorCode.E_INN_DISHES_NO_FOUND);
            return result;
        }

        int cookTime = dishesInfo.getCookSecond();
        if (cookStartTime + cookTime * userInnInfo.getDishesNum() > currentTime) {
            result.setRet(GameErrorCode.E_INN_COOK_NOT_END);
            return result; //经营还没结束
        }
        //厨艺值处理
        endCookExpHandel(currentTime);
        //钱箱处理
        endCookBoxHandel(dishesInfo);
        //菜品处理
        endCookDishesHandel(dishesInfo);
        //评价系统处理
        endCookScoreHandel(result, dishesInfo);

        //重置数据
        clearUserInnInfo();

        //触发现金礼包
        player.notifyListener(eGamePlayerEventType.RechargeInn.getValue(), 0);
        return result;
    }

    /**
     * 点击钱箱
     *
     * @return
     */
    public Property getBoxReward() {
        long resultNum;
        //获取钱箱之前的钱
        resultNum = userInnInfo.getLastValue();

        long currentTime = System.currentTimeMillis() / 1000;

        long endTime = getCookEndTime();
        //当前是否开始营业 营业中还有未领取的交子
        if (isCook() && userInnInfo.getLastClickMoneyTime() < endTime) {
            long cookStartTime = userInnInfo.getCookStartTime();
            long lastClickMoneyTime = userInnInfo.getLastClickMoneyTime();

            if (currentTime < endTime) {
                endTime = currentTime;
            }
            long second = 0;
            //  lastClick 小于烹饪开始时间
            if (lastClickMoneyTime < cookStartTime) {
                second = endTime - cookStartTime;
            } else {
                second = endTime - lastClickMoneyTime;
            }

            if (second > 0) {
                DishesInfo dishes = DishesMgr.getDishes(userInnInfo.getDishesId());
                //在经营期间的收益
                long cookNum = dishes.getMoneyPerSnd() * second;
                //加上之前剩余的
                resultNum = resultNum + cookNum;
            }
        }

        //更新点击时间
        userInnInfo.setLastClickMoneyTime(currentTime);

        //没有钱
        if (resultNum == 0) {
            return null;
        }
        Property property = new Property(eItemId.INN_GAME_MONEY_INN.getValue(), BigInteger.valueOf(resultNum));
        //调用接口增加交子数
        player.getModule(CurrencyModule.class).addCurrency(property, eLogMoneyType.Inn, eLogMoneyType.InnBoxReward);

        userInnInfo.setLastValue(0);

        return property;
    }

    /**
     * 菜品技能升级
     *
     * @param dishesId
     * @return
     */
    public short dishesUpgrade(int dishesId) {
        UserDishes dishes = userDishesMap.get(dishesId);
        DishesInfo dishesMgr = DishesMgr.getDishes(dishesId);
        if (null == dishes || null == dishesMgr) {
            return GameErrorCode.E_INN_DISHES_NO_FOUND;
        }

        int level = dishes.getLevel();

        SkillInfo skillInfo = SkillMgr.getSkillInfo(dishesMgr.getSkillId());

        if (null == skillInfo) {
            return GameErrorCode.E_INN_DISHES_SKILL_NO_FOUND;
        }

        SkillUpgradeInfo skillUpgradeInfo = SkillMgr.getSkillUpgradeInfo(skillInfo.getUpgradeType(0), level + 1);
        if (null == skillUpgradeInfo) {
            return GameErrorCode.E_INN_DISHES_LEVEL_MAX;         //最大限制
        }
        SkillUpgradeInfo currSkillUpgradeInfo = SkillMgr.getSkillUpgradeInfo(skillInfo.getUpgradeType(0), level);

        if (null == currSkillUpgradeInfo) {
            return GameErrorCode.E_INN_DISHES_LEVEL_MAX;       //没有数据 错误
        }

        if (dishes.getCurrentCookNum() < currSkillUpgradeInfo.getConsumeCount()) {
            return GameErrorCode.E_INN_DISHES_COOK_NUM_NO_ENOUGH;  //次数不够
        }

        dishes.setLevel(level + 1);
        //单个菜品数据刷新
        List<UserDishes> list = new ArrayList<>();
        list.add(dishes);
        syncDishesData(list, false);
        //刷新全部门客实力
        player.getModule(PatronsModule.class).onPatronsChange(null);
        //日志
        AutoLogMgr.add(new LogDishesUpgrade(player.getUserId(), dishesId, dishesMgr.getDishesType(), level, dishes.getLevel()));
        return 0;
    }

    /**
     * 菜品一键升级所有到所有
     */
    public short dishesUpgradeOneKey() {

        InnProto.InnDishesUpgradeOneKeyRspMsg.Builder clientMsg = InnProto.InnDishesUpgradeOneKeyRspMsg.newBuilder();
        clientMsg.setRet(0);
        //单个菜品数据刷新
        List<UserDishes> list = new ArrayList<>();
        for (Map.Entry<Integer, UserDishes> entry : userDishesMap.entrySet()) {
            UserDishes dishes = entry.getValue();
            int dishesId = dishes.getDishesId();
            DishesInfo dishesInfo = DishesMgr.getDishes(dishesId);
            if(dishesInfo == null){
                continue;
            }
            SkillInfo skillInfo = SkillMgr.getSkillInfo(dishesInfo.getSkillId());
            if(skillInfo == null){
                continue;
            }
            int oldLv = dishes.getLevel();
            int addLv = 0;
            for (int i = 1; i < 100; i++) {
                SkillUpgradeInfo skillUpgradeInfo = SkillMgr.getSkillUpgradeInfo(skillInfo.getUpgradeType(0), dishes.getLevel() + i);
                if(skillUpgradeInfo == null){
                    break;
                }

                SkillUpgradeInfo currSkillUpgradeInfo = SkillMgr.getSkillUpgradeInfo(skillInfo.getUpgradeType(0), dishes.getLevel() + i - 1);
                if(currSkillUpgradeInfo == null){
                    break;
                }
                if(currSkillUpgradeInfo.getConsumeCount() > dishes.getCurrentCookNum()){
                    break;
                }
                addLv++;
            }

            if(addLv > 0){
                //日志
                AutoLogMgr.add(new LogDishesUpgrade(player.getUserId(), dishesId, dishesInfo.getDishesType(), oldLv, oldLv + addLv));
                dishes.setLevel(dishes.getLevel() + addLv);
                list.add(dishes);
                InnProto.InnDishesUpgradeTemp.Builder builder = InnProto.InnDishesUpgradeTemp.newBuilder();
                builder.setDishesId(dishesId);
                builder.setNowLv(dishes.getLevel());
                builder.setOldLv(oldLv);
                clientMsg.addResult(builder);
            }
        }
        if(list.size() > 0){
            syncDishesData(list, false);
            //刷新全部门客实力
            player.getModule(PatronsModule.class).onPatronsChange(null);
        }
        if(list.size() == 0){
            clientMsg.setRet(GameErrorCode.E_INN_DISHES_COOK_NUM_NO_ENOUGH);
        }
        player.sendPacket(Protocol.U_INN_DISHES_UPGRADE_ONE_KEY, clientMsg);
        return 0;
    }

    /**
     * 门客计算获取玩家菜品信息
     *
     * @return
     */
    public Map<Integer, UserDishes> getUserDishesMap() {
        if (null == userDishesMap) {
            return null;
        }
        return new ConcurrentHashMap<>(userDishesMap);
    }

    /*
     获取玩家累计烹饪次数
     */
    public long getUserTotalCookTimes() {
        if (null == userInnInfo) {
            return 0;
        }
        return userInnInfo.getTotalCookTimes();
    }


    //设置开始烹饪数据
    private void startUserCookInfo(DishesInfo dishes, PatronsInfo patronsInfo, int num) {
        long currentTime = System.currentTimeMillis() / 1000;
        if (userInnInfo == null) {
            userInnInfo = initUserCookInfo();
        }
        userInnInfo.setUserId(player.getUserId());
        userInnInfo.setPatronsId(patronsInfo.getId());
        userInnInfo.setDishesId(dishes.getDishesId());
        userInnInfo.setCookStartTime(currentTime);
        userInnInfo.setDishesNum(num);
        userInnInfo.setHotRate(-1);
        //userInnInfo.setLastClickMoneyTime(0);
    }

    //获取每日热菜
    private Map<Integer, Integer> getHotDishes() {
        //周几　１－７
        int weekIndex = DateHelper.getTodayWeek();
        return DishesMgr.getHootDishes(weekIndex);
    }


    //获取本次烹饪结束后可获厨艺值(菜品厨艺值+加成厨艺值)
    private long getEndCookExp() {
        DishesInfo dishesInfo = DishesMgr.getDishes(userInnInfo.getDishesId());
        if (isCook() && userInnInfo.getEndCookExp() == 0) {
            //菜品厨艺值
            BigInteger addCookExp = PropertyHelper.parseStringToProperty(dishesInfo.getRewords()).getGoods().get(GameConfig.COOKING_ITEM_ID);
            if (null != addCookExp) {
                //获取厨艺值=单个菜品厨艺值*菜品数量*（1+当日是否有热菜加成+技能类BUFF效果）  向上取整
                BigDecimal percent = BigDecimal.valueOf(1);
                if (userInnInfo.isHot()) {
                    percent = percent.add(BigDecimal.valueOf(userInnInfo.getHotRate()).divide(BigDecimal.valueOf(1000)));
                }
                percent = percent.add(BigDecimal.valueOf(CurioMgr.getSkillAdditionBySkillType(eSkillType.CurioCookAddSkill.getValue(), player, 0)).divide(BigDecimal.valueOf(1000), 2, BigDecimal.ROUND_UP));
                addCookExp = new BigDecimal(addCookExp).multiply(BigDecimal.valueOf(userInnInfo.getDishesNum()))
                        .multiply(percent).setScale(0, BigDecimal.ROUND_UP).toBigInteger();
                userInnInfo.setEndCookExp(addCookExp.longValue());
            }
        }
        return userInnInfo.getEndCookExp();
    }


    //同步用户菜铺数据
    private void syncAllDishesData() {
        if (null != userDishesMap && userDishesMap.size() > 0) {
            //同步一下菜品信息
            syncDishesData(new ArrayList<>(userDishesMap.values()), true);
        }
    }

    //初始化用户数据
    private UserInnInfo initUserCookInfo() {
        UserInnInfo userCookInfo = new UserInnInfo();
        userCookInfo.setUserId(player.getUserId());
        userCookInfo.setInsertOption();
        return userCookInfo;
    }

    //是否烹饪状态
    private boolean isCook() {
        if (null == userInnInfo) {
            return false;
        }
        return userInnInfo.getCookStartTime() > 0;
    }


    //获取本次烹饪结束时间
    private long getCookEndTime() {
        if (null == userInnInfo || !isCook()) return 0;
        if (userInnInfo.getEndCookTime() == 0) {
            //开始时间+时长*数量
            long endTime = userInnInfo.getCookStartTime() + DishesMgr.getDishes(userInnInfo.getDishesId()).getCookSecond() * userInnInfo.getDishesNum();
            userInnInfo.setEndCookTime(endTime);
        }
        return userInnInfo.getEndCookTime();
    }

    //同步客栈信息
    private void syncUserCookInfo() {
        InnProto.InnDetailInfoMsg innInfo = InnPb.parseInnInfo(userInnInfo, getEndCookExp());
        if (null != innInfo) {
            player.sendPacket(Protocol.U_INN_SYNC, innInfo.toBuilder());
        }
    }

    //同步菜谱数据
    private void syncDishesData(List<UserDishes> userDishes, boolean isLogin) {
        if (isLogin) {
            player.sendPacket(Protocol.U_DISHES_INFO, InnPb.parseDishesListMsg(userDishes));
        } else {
            player.sendPacket(Protocol.U_DISHES_SYNC, InnPb.parseDishesListMsg(userDishes));
        }
    }

    //烹饪结束重置数据
    private void clearUserInnInfo() {
        if (null != userInnInfo) {
            userInnInfo.setCookStartTime(0);
            userInnInfo.setPatronsId(0);
            userInnInfo.setDishesNum(0);
            userInnInfo.setDishesId(0);
            userInnInfo.setHotRate(-1);
            userInnInfo.setEndCookExp(0);
            userInnInfo.setEndCookTime(0);
        }
    }

    /**
     * 烹饪结束厨艺值
     *
     * @param currentTime
     */
    private void endCookExpHandel(long currentTime) {
        UserPatrons userPatrons = player.getModule(PatronsModule.class).getUserPatrons(userInnInfo.getPatronsId());
        if (userPatrons != null) {
            //前值
            long preCookExp = userPatrons.getCookExp();
            //变化值
            long changeNum = getEndCookExp();

            userPatrons.setCookExp(preCookExp + changeNum);
            //更新门客数据
            player.getModule(PatronsModule.class).syncPatronsData(userPatrons);

            // 厨艺值日志
            AutoLogMgr.add(new LogPatronsCook(
                    player.getUserId(),
                    userPatrons.getPatronsId(),
                    true,
                    preCookExp,
                    changeNum,
                    userPatrons.getCookExp()
            ));
        }
    }

    /**
     * 烹饪结束钱箱
     *
     * @param dishesInfo
     */
    private void endCookBoxHandel(DishesInfo dishesInfo) {
        //客栈钱箱数量结算为last value
        long lastClickMoneyTime = userInnInfo.getLastClickMoneyTime();

        long cookRemainMoney;
        if (lastClickMoneyTime < userInnInfo.getCookStartTime()) {
            //烹饪期间未点击过
            cookRemainMoney = dishesInfo.getMoneyPerSnd() * userInnInfo.getDishesNum() * dishesInfo.getCookSecond();

        } else {
            //结束时间减去上次点击时间
            cookRemainMoney = dishesInfo.getMoneyPerSnd() * (getCookEndTime() - userInnInfo.getLastClickMoneyTime());
        }

        if (cookRemainMoney > 0) {
            userInnInfo.setLastValue(cookRemainMoney + userInnInfo.getLastValue());
        }
    }

    /**
     * 烹饪结束菜品
     *
     * @param dishesInfo
     */
    private void endCookDishesHandel(DishesInfo dishesInfo) {
        //菜品次数增加
        UserDishes userDishes = userDishesMap.get(userInnInfo.getDishesId());
        boolean bInit = false;
        if (null == userDishes) {
            //初始化玩家菜品
            userDishes = new UserDishes();
            userDishes.setCurrentCookNum(0);
            userDishes.setDishesId(dishesInfo.getDishesId());
            userDishes.setUserId(player.getUserId());
            userDishes.setLevel(1); //初始1级
            userDishes.setInsertOption();//插入
            userDishesMap.put(dishesInfo.getDishesId(), userDishes);
            bInit = true;
        }
        userDishes.setCurrentCookNum(userDishes.getCurrentCookNum() + userInnInfo.getDishesNum());

        int dishesNum = userInnInfo.getDishesNum();

        //单个菜品数据刷新
        List<UserDishes> list = new ArrayList<>();
        list.add(userDishes);
        syncDishesData(list, false);
        //菜品技能加成刷新
        if (bInit) {
            player.getModule(PatronsModule.class).onPatronsChange(null);
        }

        //成就、任务
        player.notifyListener(eGamePlayerEventType.UnionInnCookTimes.getValue(), new UnionActivityTaskArgs(UnionActivityTaskArgs.TYPE_ADD_VALUE, dishesNum, player.getUserInfo().getUnionUid()));
    }

    /**
     * 烹饪结束评价
     *
     * @param result
     * @param dishesInfo
     */
    private void endCookScoreHandel(InnEndCookResult result, DishesInfo dishesInfo) {

        Random random = new Random();

        int quality = dishesInfo.getQuality();

        List<Integer> dishesQualityScoreList = ConfigMgr.getDishesQualityScoreList();

        int level = userDishesMap.get(userInnInfo.getDishesId()).getLevel();

        Integer baseScore = dishesQualityScoreList.get(quality - 1);
        if (baseScore != null) {
            int dishesLevelScoreParam1 = ConfigMgr.getDishesLevelScoreParam1();
            int dishesLevelScoreParam2 = ConfigMgr.getDishesLevelScoreParam2();
            int dishesLevelScoreParam3 = ConfigMgr.getDishesLevelScoreParam3();

            int dishesScoreRandomParam1 = ConfigMgr.getDishesScoreRandomParam1();
            int dishesScoreRandomParam2 = ConfigMgr.getDishesScoreRandomParam2();

            List<InnScore> dishesScoreList = ConfigMgr.getDishesScoreList();


            // 菜品等级影响分数参数：600|1200|10    => 等级^（参数1/1000)*（参数2/1000）+参数3
            BigDecimal levelScore = BigDecimal.valueOf(Math.pow(level, BigDecimal.valueOf(dishesLevelScoreParam1).divide(BigDecimal.valueOf(1000)).doubleValue()))
                    .multiply(BigDecimal.valueOf(dishesLevelScoreParam2).divide(BigDecimal.valueOf(1000)))
                    .add(BigDecimal.valueOf(dishesLevelScoreParam3)).setScale(1, BigDecimal.ROUND_UP);

            // 评分=菜品品质基础分数*菜品等级系数*随机概率区间

            int randomNum = random.nextInt(dishesScoreRandomParam2 - dishesScoreRandomParam1 + 1) + dishesScoreRandomParam1;
            int resultScore = BigDecimal.valueOf(baseScore)
                    .multiply(levelScore)
                    .multiply(BigDecimal.valueOf(randomNum).divide(BigDecimal.valueOf(1000))).setScale(0, BigDecimal.ROUND_UP).intValue();

            result.setScore(resultScore);
            // 菜品评分效果：50;0|100;30|200;50|300;100|9999;150      =>   50分以下+0% 100分以下+3%
            int scoreAddition = 0;
            for (int i = 0; i < dishesScoreList.size(); i++) {
                InnScore innScore = dishesScoreList.get(i);
                if (resultScore <= innScore.getScore()) {
                    scoreAddition = innScore.getValue();
                    //分数等级
                    result.setScoreLevel(i);
                    break;
                }
            }

            if (scoreAddition > 0) {
                //3 交子打赏=菜品每秒交子数*营业时间*加成系数
                BigInteger rewardNum = BigDecimal.valueOf(dishesInfo.getMoneyPerSnd())
                        .multiply(BigDecimal.valueOf(dishesInfo.getCookSecond()))
                        .multiply(BigDecimal.valueOf(userInnInfo.getDishesNum()))
                        .multiply(BigDecimal.valueOf(scoreAddition).divide(BigDecimal.valueOf(1000))).setScale(0, BigDecimal.ROUND_UP).toBigInteger();

                if (rewardNum != null && rewardNum.compareTo(BigInteger.ZERO) > 0) {
                    Property resultReword = new Property();
                    resultReword.addProperty(eItemId.INN_GAME_MONEY_INN.getValue(), rewardNum);
                    player.getModule(CurrencyModule.class).addCurrency(resultReword, eLogMoneyType.Inn, eLogMoneyType.InnBoxReward);
                    result.setReward(PropertyHelper.parsePropertyToString(resultReword));
                }
            }
        }
    }
}
